<?php

namespace Kunfeng\Dify\Model\Datasets;

/**
 * 创建知识库请求模型
 */
class CreateDatasetRequest
{
    private $name;
    private $description;
    private $indexingTechnique;
    private $permission;
    private $provider;

    public function __construct(
        string $name,
        ?string $description = null,
        string $indexingTechnique = 'high_quality',
        string $permission = 'only_me',
        string $provider = 'vendor'
    ) {
        $this->name = $name;
        $this->description = $description;
        $this->indexingTechnique = $indexingTechnique;
        $this->permission = $permission;
        $this->provider = $provider;
    }

    /**
     * 创建请求
     */
    public static function create(string $name): self
    {
        return new self($name);
    }

    /**
     * 使用建造者模式
     */
    public static function builder(): CreateDatasetRequestBuilder
    {
        return new CreateDatasetRequestBuilder();
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        $data = [
            'name' => $this->name,
            'indexing_technique' => $this->indexingTechnique,
            'permission' => $this->permission,
            'provider' => $this->provider,
        ];

        if ($this->description !== null) {
            $data['description'] = $this->description;
        }

        return $data;
    }

    // Getters
    public function getName(): string
    {
        return $this->name;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function getIndexingTechnique(): string
    {
        return $this->indexingTechnique;
    }

    public function getPermission(): string
    {
        return $this->permission;
    }

    public function getProvider(): string
    {
        return $this->provider;
    }
}

/**
 * 创建知识库请求构建器
 */
class CreateDatasetRequestBuilder
{
    private $name;
    private $description;
    private $indexingTechnique = 'high_quality';
    private $permission = 'only_me';
    private $provider = 'vendor';

    public function name(string $name): self
    {
        $this->name = $name;
        return $this;
    }

    public function description(string $description): self
    {
        $this->description = $description;
        return $this;
    }

    public function indexingTechnique(string $indexingTechnique): self
    {
        $this->indexingTechnique = $indexingTechnique;
        return $this;
    }

    public function permission(string $permission): self
    {
        $this->permission = $permission;
        return $this;
    }

    public function provider(string $provider): self
    {
        $this->provider = $provider;
        return $this;
    }

    public function build(): CreateDatasetRequest
    {
        if (!isset($this->name)) {
            throw new \InvalidArgumentException('name is required');
        }

        return new CreateDatasetRequest(
            $this->name,
            $this->description,
            $this->indexingTechnique,
            $this->permission,
            $this->provider
        );
    }
}
